package com.cskaoyan.user.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.util.CookieUtil;
import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.dto.*;
import com.cskaoyan.user.service.IKaptchaService;
import com.cskaoyan.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author yangzihui
 * @Description 用户模块的controller
 * @Create 2022-07-08 19:41
 * @Version
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private IKaptchaService iKaptchaService;

    @Autowired
    private IUserService iUserService;

    /**
     * 用户注册的方法
     *
     * @param map
     * @param request
     * @return
     */
    @PostMapping("register")
    public ResponseData register(@RequestBody Map<String, String> map, HttpServletRequest request) {
        // 获取请求参数
        String userName = map.get("userName");
        String userPwd = map.get("userPwd");
        String captcha = map.get("captcha");
        String email = map.get("email");

        // 验证验证码
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        String uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        kaptchaCodeRequest.setUuid(uuid);
        kaptchaCodeRequest.setCode(captcha);
        KaptchaCodeResponse kaptchaCodeResponse = iKaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        String code = kaptchaCodeResponse.getCode();
        if (!code.equals(UserRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setErrorMsg(kaptchaCodeResponse.getMsg());
        }

        // 向用户表中插入记录
        UserRegisterRequest userRegisterRequest = new UserRegisterRequest();
        userRegisterRequest.setUserName(userName);
        userRegisterRequest.setUserPwd(userPwd);
        userRegisterRequest.setEmail(email);
        UserRegisterResponse userRegisterResponse = iUserService.register(userRegisterRequest);
        if (userRegisterResponse.getCode().equals(UserRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(null);
        }
        return new ResponseUtil<>().setErrorMsg(userRegisterResponse.getMsg());
    }

    /**
     * 用户登录的post方法
     *
     * @param map
     * @param request
     * @return
     */
    @PostMapping("login")
    public ResponseData login(@RequestBody Map<String, String> map, HttpServletRequest request, HttpServletResponse response) {
        // 获取请求参数
        String userName = map.get("userName");
        String userPwd = map.get("userPwd");
        String captcha = map.get("captcha");

        // 验证验证码
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        String uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        kaptchaCodeRequest.setUuid(uuid);
        kaptchaCodeRequest.setCode(captcha);
        KaptchaCodeResponse kaptchaCodeResponse = iKaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        String code = kaptchaCodeResponse.getCode();
        if (!code.equals(UserRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setErrorMsg(kaptchaCodeResponse.getMsg());
        }

        // 用户登录
        UserLoginRequest userLoginRequest = new UserLoginRequest();
        userLoginRequest.setUserName(userName);
        userLoginRequest.setPassword(userPwd);
        UserLoginResponse userLoginResponse = iUserService.login(userLoginRequest);
        if (userLoginResponse.getCode().equals(UserRetCode.SUCCESS.getCode())) {
            Cookie cookie = new Cookie("access_token", userLoginResponse.getToken());
            cookie.setPath("/");
            response.addCookie(cookie);
            return new ResponseUtil<>().setData(userLoginResponse);
        }
        return new ResponseUtil<>().setErrorMsg(userLoginResponse.getMsg());
    }

    /**
     * 验证用户登录的get方法
     *
     * @param request
     * @return
     */
    @GetMapping("login")
    public ResponseData loginVerify(HttpServletRequest request) {
        // 获取user_info请求头
        String userInfoJson = request.getHeader("user_info");
        // 解析json字符串
        JSONObject userInfoJsonObject = JSON.parseObject(userInfoJson);
        UserLoginVerifyRequest userLoginVerifyRequest = JSON.toJavaObject(userInfoJsonObject, UserLoginVerifyRequest.class);
        UserLoginVerifyResponse userLoginVerifyResponse = iUserService.loginVerify(userLoginVerifyRequest);
        if (userLoginVerifyResponse.getCode().equals(UserRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(userLoginVerifyResponse);
        }
        return new ResponseUtil<>().setErrorMsg(userLoginVerifyResponse.getMsg());
    }

    /**
     * 用户注册激活的方法
     *
     * @param username
     * @param uuid
     * @return
     */
    @GetMapping("verify")
    public String verify(String username, String uuid) {
        UserVerifyRequest userVerifyRequest = new UserVerifyRequest();
        userVerifyRequest.setUserName(username);
        userVerifyRequest.setUuid(uuid);
        UserVerifyResponse userVerifyResponse = iUserService.verify(userVerifyRequest);
        if (userVerifyResponse.getCode().equals(UserRetCode.SUCCESS.getCode())) {
            return "激活成功";
        }
        return userVerifyResponse.getMsg();
    }

    @GetMapping("loginOut")
    public ResponseData loginOut(HttpServletResponse response) {
        Cookie cookie = new Cookie("access_token", "xxx");
        cookie.setMaxAge(0);
        cookie.setPath("/");
        response.addCookie(cookie);
        return new ResponseUtil<>().setData(null);
    }
}
